/*
 * KeyboardInputHandler.java
 *
 * Created on January 22, 2007, 8:46 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.sf.iSnake.core;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 *
 * @author thelinuxmaniac
 *
 * Implents the functionality to control the snake from the keyboard direction keys (up,right,down,left)
 *
 */
public class KeyboardInputHandler extends SnakeInputHandler implements KeyListener{
    
    private Short player_id;
    private Short dxForSending;             // this dx will be transmitted to the server
    private Short dyForSending;             // this dy will be transmitted to the server
    
    private boolean debug;
    private boolean startTrappingDirectionKeys;
    private boolean stopKeyboardInputHandler;
    private boolean sendKeystroke;
    private boolean dxdyChanged;

    private postmodeISCPpacketForSend postmodeSend;
    private ClientNetworkInterface clientNetworkInterface;
            
    /** Creates a new instance of KeyboardInputHandler */
    public KeyboardInputHandler() {
        super("Local Keyboard");
        dxForSending = new Short("0");
        dyForSending = new Short("0");
        setDebug(false);                                // supress debugging messages by default
        postmodeSend = new postmodeISCPpacketForSend(ISCP.POST_CMD_SEND_dxdy);
    }

    /*
     * This part of the code was provided by Jitendra
     */
    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
        if(getDebug())    {
            System.out.println("Key pressed was: "+e.getKeyText(e.getKeyCode())+"+isDxdyChanged()= "+isDxdyChanged());
        }
        if(e.getKeyCode() == e.VK_UP){
//            System.out.println("up");
            if(dxForSending.equals(new Short("0")) && (dyForSending.equals(new Short("-1")) || dyForSending.equals(new Short("1")))) {
                setDxdyChanged(false);
            }
            else    {
                dxForSending = 0;
                dyForSending = -1;

                setDxdyChanged(true);

            }
        }
        else if(e.getKeyCode()==e.VK_DOWN){
//            System.out.println("down");
            if(dxForSending.equals(new Short("0")) && ((dyForSending.equals(new Short("1")) || dyForSending.equals(new Short("-1"))))) {
                setDxdyChanged(false);
            }
            else    {
                dxForSending = 0;
                dyForSending = 1;
                setDxdyChanged(true);
            }
        }
        else if(e.getKeyCode()==e.VK_RIGHT){
//            System.out.println("right");
            if((dxForSending.equals(new Short("1")) || dxForSending.equals(new Short("-1"))) && dyForSending.equals(new Short("0"))) {
                setDxdyChanged(false);
            }
            else    {
                dxForSending = 1;
                dyForSending = 0;
                setDxdyChanged(true);
            }
        }
        else if(e.getKeyCode()==e.VK_LEFT){
//            System.out.println("left");
            if((dxForSending.equals(new Short("-1")) || dxForSending.equals(new Short("1"))) && dyForSending.equals(new Short("0"))) {
                setDxdyChanged(false);
            }
            else    {
                dxForSending = -1;
                dyForSending = 0;
                setDxdyChanged(true);
            }
        }
    }

    public void keyReleased(KeyEvent e) {
    }

    public boolean isDebug() {
        return debug;
    }

    public void setDebug(boolean debug) {
        this.debug = debug;
    }

    public boolean getDebug() {
        return this.isDebug();
    }

    public boolean isDxdyChanged() {
        return dxdyChanged;
    }

    public void setDxdyChanged(boolean dxdyChanged) {
        this.dxdyChanged = dxdyChanged;
    }

    public Short getPlayer_id() {
        return player_id;
    }

    public void setPlayer_id(Short player_id) {
        this.player_id = player_id;
    }
    
    public Short getDxForSending() {
        return dxForSending;
    }

    public void setDxForSending(Short dxForSending) {
        this.dxForSending = dxForSending;
    }

    public Short getDyForSending() {
        return dyForSending;
    }

    public void setDyForSending(Short dyForSending) {
        this.dyForSending = dyForSending;
    }
    
    /**
     * This will start trapping the input keystokes and send it to the game server and then enter wait mode until thread is woken up again
     */
    public synchronized void run() {
        System.out.println("keyboard input handler thread started");
        while(!isStopKeyboardInputHandler())    {
            if(isStartTrappingDirectionKeys() && isSendKeystroke())  {
//                System.out.println("checking for dx dy change");
                if(isDxdyChanged()) {
                    System.out.println("sending changed keystroke to server: dx="+getDxForSending()+" dy="+getDyForSending());
                    getPostmodeSend().setPlayer_id(getPlayer_id());
                    getPostmodeSend().setPostmodeCommandSend(ISCP.POST_CMD_SEND_dxdy);
                    getPostmodeSend().setDxDy(getDxForSending(),getDyForSending());
//                    postmodeInputKeysPacket.setDxDy(new Short("1"),new Short("1"));
                    getClientNetworkInterface().send(getPostmodeSend());
                    System.out.println("sent values of dx= "+getPostmodeSend().getDx()+", dy= "+getPostmodeSend().getDy());
                }
                // do nothing if the input keystokes have not changed
                setSendKeystroke(false);
            }
            else    {
                try {
                    System.out.println("keyboard input handler waiting");    
                    wait();
                    System.out.println("keyboard input handler NOT waiting");
                } catch (InterruptedException ex) {
                        ex.printStackTrace();
                }
            }
        }
    }

    public synchronized void myNotifier()   {
        notifyAll();
    }
    public boolean isStartTrappingDirectionKeys() {
        return startTrappingDirectionKeys;
    }

    public void setStartTrappingDirectionKeys(boolean startTrappingDirectionKeys) {
        this.startTrappingDirectionKeys = startTrappingDirectionKeys;
    }

    public boolean isStopKeyboardInputHandler() {
        return stopKeyboardInputHandler;
    }

    public void setStopKeyboardInputHandler(boolean stopKeyboardInputHandler) {
        this.stopKeyboardInputHandler = stopKeyboardInputHandler;
    }

    public short getDX() {
        return dxForSending;
    }

    public short getDY() {
        return dyForSending;
    }

    public boolean isSendKeystroke() {
        return sendKeystroke;
    }

    public void setSendKeystroke(boolean sendKeystroke) {
        this.sendKeystroke = sendKeystroke;
    }

    public postmodeISCPpacketForSend getPostmodeSend() {
        return postmodeSend;
    }

    public void setPostmodeSend(postmodeISCPpacketForSend postmodeSend) {
        this.postmodeSend = postmodeSend;
    }

    public ClientNetworkInterface getClientNetworkInterface() {
        return clientNetworkInterface;
    }

    public void setClientNetworkInterface(ClientNetworkInterface clientNetworkInterface) {
        this.clientNetworkInterface = clientNetworkInterface;
    }
}